package com.imooc.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 * 跨越配置
 * @author: bizy
 * @date: 2020/06/21 18:12
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final  CorsConfiguration config = new CorsConfiguration();

        config.setAllowCredentials(true);               //是否支持跨越
        config.setAllowedOrigins(Arrays.asList("*"));   //原始域 http://www.a.com
        config.setAllowedHeaders(Arrays.asList("*"));   //允许头
        config.setAllowedMethods(Arrays.asList("*"));   //允许方法
        config.setMaxAge(300l);                         //缓存时间

        source.registerCorsConfiguration("/**",config);
        return new CorsFilter(source);
    }

}
